package com.luobo.home.service;

import com.luobo.home.pojo.Question;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface QuestionMapper {
//    @Select("SELECT * FROM questions WHERE id > #{currentId}  ORDER BY id ASC LIMIT 1")
//    Question findNextQuestion(Long currentId);
//
//    @Select({
//            "<script>",
//            "SELECT * FROM questions",
//            "<where>",
//            "   <if test='excludedIds != null and excludedIds.size() > 0'>",
//            "       id NOT IN ",
//            "       <foreach item='id' collection='excludedIds' open='(' separator=',' close=')'>",
//            "           #{id}",
//            "       </foreach>",
//            "   </if>",
//            "</where>",
//            "ORDER BY appearCount ASC, correctCount ASC,incorrectCount DESC, RAND() LIMIT 1",
//            "</script>"
//    })
////    List<Question> findCandidates(@Param("excludedIds") List<Long> excludedIds);
//    Question findCandidates(@Param("excludedIds") List<Long> excludedIds);

    @Update("UPDATE questions SET appearCount = #{appearCount} WHERE id = #{id}")
    void updateAppearCount(Question question);

    @Update("UPDATE questions SET correctCount = #{correctCount} WHERE id = #{id}")
    void updateCorrectCount(Question question);

    @Update("UPDATE questions SET incorrectCount = #{incorrectCount} WHERE id = #{id}")
    void updateIncorrectCount(Question question);

    @Select("SELECT * FROM questions WHERE id = #{id}")
    Question findById(Long id);

    // 所有查询方法增加bankId参数
    @Select("SELECT * FROM questions WHERE bankId = #{currentBankId} AND id > #{currentId} ORDER BY id ASC LIMIT 1")
    Question findNextQuestion(@Param("currentId") Long currentId,
                              @Param("currentBankId") Integer currentBankId);

    @Select({
            "<script>",
            "SELECT * FROM questions",
            "<where>",
            "   bankId = #{currentBankId}  ",
            "   <if test='excludedIds != null and excludedIds.size() > 0'>",
            "       and id NOT IN ",
            "       <foreach item='id' collection='excludedIds' open='(' separator=',' close=')'>",
            "           #{id}",
            "       </foreach>",
            "   </if>",
            "</where>",
            "ORDER BY appearCount ASC, correctCount ASC,incorrectCount DESC, RAND() LIMIT 1",
            "</script>"
    })
//    List<Question> findCandidates(@Param("excludedIds") List<Long> excludedIds);
    Question findCandidates(@Param("excludedIds") List<Long> excludedIds,
                            @Param("currentBankId") Integer currentBankId);

}